package com.glorysoft.wangnianxinpda.adapter;

import android.content.Context;

import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.glorysoft.wangnianxinpda.R;
import com.glorysoft.wangnianxinpda.entity.BasShortcutIconInfoEntity;

import java.util.List;

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
    private Context mContext; // 上下文
    private int layout_item;
    private List<BasShortcutIconInfoEntity> mBasShortcupTconInfoList;

    public RecyclerViewAdapter(int layout_item, List<BasShortcutIconInfoEntity> mBasShortcupTconInfoList) {
        this.layout_item = layout_item;
        this.mBasShortcupTconInfoList = mBasShortcupTconInfoList;
    }

    public RecyclerViewAdapter(Context context, int layout_item, List<BasShortcutIconInfoEntity> mBasShortcupTconInfoList) {
        this(layout_item, mBasShortcupTconInfoList);
        this.mContext = context;
    }

    @Override
    public int getItemCount() {
        return mBasShortcupTconInfoList == null ? 0 : mBasShortcupTconInfoList.size();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(layout_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {

        String sText = mBasShortcupTconInfoList.get(position).getText();//图标名称
        String sShortcutPictureName = mBasShortcupTconInfoList.get(position).getPicName();//图标命名
        String sShortcutPictureTag = mBasShortcupTconInfoList.get(position).getShowPlugin();//对应的Activity

        int nShortcutPictureId = mContext.getResources().getIdentifier(sShortcutPictureName, "drawable", mContext.getPackageName());

        viewHolder.itemView.setBackgroundResource(R.color.transparent);
        viewHolder.itemView.setTag(sShortcutPictureTag);
        viewHolder.mImageView.setImageResource(nShortcutPictureId); //mInts.get(0)
        viewHolder.mTextView.setText(sText);

    }

    public List<BasShortcutIconInfoEntity> getDataList() {
        return mBasShortcupTconInfoList;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mTextView;
        public ImageView mImageView;

        ViewHolder(View itemView) {
            super(itemView);
            mTextView = itemView.findViewById(R.id.tv_operate_name);
            mImageView = itemView.findViewById(R.id.iv_operate_img);
        }
    }
}
